package com.spic.business.safetyactivity.service.impl;

import com.spic.business.safetyactivity.domain.SafetyActivity;
import com.spic.business.safetyactivity.domain.SafetyActivitySign;
import com.spic.business.safetyactivity.mapper.SafetyActivitySignMapper;
import com.spic.business.safetyactivity.service.ISafetyActivityService;
import com.spic.business.safetyactivity.service.ISafetyActivitySignService;
import com.spic.common.core.domain.R;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.SecurityUtils;
import com.spic.system.api.RemoteUserService;
import com.spic.system.api.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 安全活动签到人员Service业务层处理
 * 
 * @author spic
 * @date 2022-06-14
 */
@Service
public class SafetyActivitySignServiceImpl implements ISafetyActivitySignService
{
    @Autowired
    private SafetyActivitySignMapper safetyActivitySignMapper;
    @Autowired
    private RemoteUserService remoteUserService;
    @Autowired
    private ISafetyActivityService safetyActivityService;

    /**
     * 查询安全活动签到人员
     * 
     * @param id 安全活动签到人员主键
     * @return 安全活动签到人员
     */
    @Override
    public SafetyActivitySign selectSafetyActivitySignById(String id)
    {
        return safetyActivitySignMapper.selectSafetyActivitySignById(id);
    }

    /**
     * 查询安全活动签到人员列表
     * 
     * @param safetyActivitySign 安全活动签到人员
     * @return 安全活动签到人员
     */
    @Override
    public Map<String, List<SafetyActivitySign>> selectSafetyActivitySignList(SafetyActivitySign safetyActivitySign)
    {
        List<SafetyActivitySign> list = safetyActivitySignMapper.selectSafetyActivitySignList(safetyActivitySign);
        list.stream().forEach(item -> {
            R<SysUser> user = remoteUserService.getUserById(Long.valueOf(item.getSignName()));
            item.setSignName(user.getData().getNickName());
        });
        Map<String, List<SafetyActivitySign>> respMap = list.stream().collect(Collectors.groupingBy(item -> "type"+ item.getType()));
        return respMap;
    }

    /**
     * 新增安全活动签到人员
     * 
     * @param safetyActivitySign 安全活动签到人员
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSafetyActivitySign(SafetyActivitySign safetyActivitySign)
    {
        Long userId = SecurityUtils.getUserId();
        SafetyActivitySign safetyActivitySign1 = new SafetyActivitySign();
        safetyActivitySign1.setActivityId(safetyActivitySign.getActivityId());
        safetyActivitySign1.setSignName(String.valueOf(userId));
        List<SafetyActivitySign> safetyActivitySigns = safetyActivitySignMapper.selectSafetyActivitySignList(safetyActivitySign1);
        if(safetyActivitySigns.size() > 0) {
            return 1;
        }
        //出勤人数+1
        SafetyActivity safetyActivity = safetyActivityService.selectSafetyActivityById(Long.valueOf(safetyActivitySign.getActivityId()));
        safetyActivity.setAttendanceNumber(safetyActivity.getAttendanceNumber() + 1);
        safetyActivityService.updateSafetyActivity(safetyActivity);

        safetyActivitySign.setSignTime(DateUtils.getNowDate());
        safetyActivitySign.setSignName(String.valueOf(userId));
        return safetyActivitySignMapper.insertSafetyActivitySign(safetyActivitySign);
    }

    /**
     * 修改安全活动签到人员
     * 
     * @param safetyActivitySign 安全活动签到人员
     * @return 结果
     */
    @Override
    public int updateSafetyActivitySign(SafetyActivitySign safetyActivitySign)
    {
        return safetyActivitySignMapper.updateSafetyActivitySign(safetyActivitySign);
    }

    /**
     * 批量删除安全活动签到人员
     * 
     * @param ids 需要删除的安全活动签到人员主键
     * @return 结果
     */
    @Override
    public int deleteSafetyActivitySignByIds(String[] ids)
    {
        return safetyActivitySignMapper.deleteSafetyActivitySignByIds(ids);
    }

    /**
     * 删除安全活动签到人员信息
     * 
     * @param id 安全活动签到人员主键
     * @return 结果
     */
    @Override
    public int deleteSafetyActivitySignById(String id)
    {
        return safetyActivitySignMapper.deleteSafetyActivitySignById(id);
    }
}
